package com.aaa.power.util;

import com.aaa.power.entity.OperLog;
import com.aaa.power.entity.User;
import com.aaa.power.service.OperLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @fileName:OperationLogUtil
 * @description:通用的日志记录类
 * @author:忆尘
 * @createTime:2020/11/12 14:14
 * @version:1.0.0
 */

@Component
@Aspect //标志位切面实现类，增强类
public class OperationLogUtil {
    /**
     * 依赖注入
     */
    @Autowired
    private OperLogService operLogService;
    /**
     * 依赖注入
     */
    @Autowired
    private HttpSession session;

/**
     * 记录日志方法
     * @param joinPoint 获取连接点信息的类
     */

    @AfterReturning(pointcut = "execution(* com.aaa.power.service.*.insert*(..))||execution(* com.aaa.power.service.*.deleteById*(..))")
    public void recordOperationLog(JoinPoint joinPoint){

        OperLog operLog = new OperLog();
        operLog.setTitle("权限管理");
        //获取执行业务的方法名称
        String methodName = joinPoint.getSignature().getName();
        int businessType = 0;
        if(methodName.startsWith("insert")) {
            businessType = 1;
        }else if(methodName.startsWith("update")){
            businessType = 2;
        }else{
            businessType = 3;
        }
        operLog.setBusinessType(businessType);
        //方法名称  类的限定名+方法名称 joinPoint.getTarget = MenuServiceImpl
        String classname = joinPoint.getTarget().getClass().getName();
        System.out.println(classname+"类的限定名+方法名称 ");
        operLog.setMethod(classname+"."+methodName+"()");
        //操作类型
        operLog.setOperatorType(1);
        //设置操作用户及部门
        User user = session.getAttribute("userInfo")==null?null:(User)session.getAttribute("userInfo");
        if(user != null){
            operLog.setOperName(user.getLoginName());
            operLog.setDeptName(user.getDeptId()+"");
        }
        //获取Request对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取Ip地址
        String remoteAddr = request.getRemoteAddr();
        operLog.setOperIp(remoteAddr);
        //获取请求地址 url= http://www.baidu.com:443/oss/dox/api/uploadapi  uri=/oss/dox/api/uploadapi
        String requestURI = request.getRequestURI();
        operLog.setOperUrl(requestURI);
        //设置内外网IP
        if(remoteAddr.startsWith("192")||remoteAddr.startsWith("::")||remoteAddr.startsWith("127")||remoteAddr.startsWith("0:0:0")){
            operLog.setOperLocation("内网IP");
        }else{
            operLog.setOperLocation("外网IP");
        }
        //设置参数
        //获取连接点的所有参数
        Object[] args = joinPoint.getArgs();
        if(args != null&&args.length > 0){
            String simpleName = "";
            //循环拼接所有参数
            for(Object arg : args){
                simpleName += arg.getClass().getSimpleName()+",";
            }
            simpleName = args[0].getClass().getSimpleName();
            operLog.setOperParam(simpleName);
        }
        //操作时间
        Date data = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        operLog.setOperTime(ft.format(data));
        operLogService.add(operLog);
    }

}
